package patrol.mobile.com.mobilepatrol.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import patrol.mobile.com.mobilepatrol.R;

/**
 * Created by apple on 17/11/15.
 */

public class TouchActivity extends Activity implements GestureDetector.OnGestureListener, View.OnTouchListener {

    private static final int FLING_MIN_DISTANCE = 50;
    private static final int FLING_MIN_VELOCITY = 0;
    GestureDetector mGestureDetector = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        View view = findViewById(R.id.layout);
        mGestureDetector = new GestureDetector(this);
        view.setOnTouchListener(this);
        //下面这几行一定要加上否则就只能识别onDown，onShowPress，和onLongPress
        view.setFocusable(true);
        view.setClickable(true);
        view.setLongClickable(true);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        Log.e("onDown", "onDown");
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.e("onShowPress", "onShowPress");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e){
        Log.e("onSingleTapUp", "onSingleTapUp");
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                            float distanceX, float distanceY) {
        Log.e("onScroll", "onScroll");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.e("onLongPress", "onLongPress");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {


        if (e1.getY()-e2.getY() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling left
            Toast.makeText(this, "向上手势", Toast.LENGTH_SHORT).show();
        } else if (e2.getY()-e1.getY() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling right
            Toast.makeText(this, "向下手势", Toast.LENGTH_SHORT).show();
        }

        Log.e("onFling", "onFling");
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }



}
